package com.hqx.gulimall.product.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.hqx.gulimall.product.entity.AttrAttrgroupRelationEntity;
import com.hqx.gulimall.product.entity.AttrEntity;
import com.hqx.gulimall.product.service.AttrAttrgroupRelationService;
import com.hqx.gulimall.product.service.CategoryService;
import com.hqx.gulimall.product.vo.AttrGroupWithAttrRespVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.hqx.gulimall.product.entity.AttrGroupEntity;
import com.hqx.gulimall.product.service.AttrGroupService;
import com.hqx.common.utils.PageUtils;
import com.hqx.common.utils.R;

import javax.annotation.Resource;


/**
 * 
 *
 * @author hqx
 * @email 2322305634@qq.com
 * @date 2024-03-14 10:44:01
 */
@RestController
@RequestMapping("product/attrgroup")
public class AttrGroupController {
    @Autowired
    private AttrGroupService attrGroupService;

    @Autowired
    private CategoryService categoryService;

    @Resource
    private AttrAttrgroupRelationService attrAttrgroupRelationService;

    /**
     * 列表
     */
    @RequestMapping("/list/{catelogId}")
    public R list(@RequestParam Map<String, Object> params,@PathVariable("catelogId") Long catelogId){
        if(catelogId == 0)
            return R.ok().put("page", attrGroupService.queryPage(params));
        return R.ok().put("page", attrGroupService.queryPage(params,catelogId));
    }

    /**
     *通过分类id获取分类下的所有分组及分组下所有属性
     */
    @RequestMapping("/attrGroupWithAttr/{catalogId}")
    public R getAttrGroupWithAttrByCatalogId(@PathVariable("catalogId") Long catelogId){
        List<AttrGroupWithAttrRespVo> attrGroupWithAttrRespVoList = attrGroupService.getAttrGroupWithAttrByCatalogId(catelogId);
        return R.ok().put("data",attrGroupWithAttrRespVoList);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{attrGroupId}")
    public R info(@PathVariable("attrGroupId") Long attrGroupId){
		AttrGroupEntity attrGroup = attrGroupService.getById(attrGroupId);
		;attrGroup.setCatelogIds(categoryService.getCatelogIdsByCatelogId(attrGroup.getCatelogId()));
        return R.ok().put("attrGroup", attrGroup);
    }

    /**
     * 通过属性分组id获取关联属性信息
     */
    @RequestMapping("/relationAttr/{attrGroupId}")
    public R getRelationAttr(@PathVariable("attrGroupId") Long attrGroupId){
        List<AttrEntity> attrEntities = attrGroupService.getRelationAttr(attrGroupId);
        return R.ok().put("data", attrEntities);
    }

    /**
     * 通过属性分组id获取未关联属性信息
     */
    @RequestMapping("/noRelationAttr/{attrGroupId}")
    public R getNoRelationAttr(@RequestParam Map<String, Object> params,@PathVariable("attrGroupId") Long attrGroupId){
        PageUtils page = attrGroupService.getNoRelationAttr(params,attrGroupId);
        return R.ok().put("page", page);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.save(attrGroup);
        return R.ok();
    }

    /**
     * 保存分组关联属性
     */
    @RequestMapping("/relationAttr/save")
    public R saveRelationAttr(@RequestBody AttrAttrgroupRelationEntity[] relationEntities){
        attrAttrgroupRelationService.saveRelationAttr(Arrays.asList(relationEntities));
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.updateById(attrGroup);
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] attrGroupIds){
		attrGroupService.removeByIds(Arrays.asList(attrGroupIds));
        return R.ok();
    }

    @RequestMapping("/relationAttr/delete")
    public R deleteRelationAttr(@RequestBody AttrAttrgroupRelationEntity[] relationEntities){
        attrGroupService.removeRelationAttr(Arrays.asList(relationEntities));
        return R.ok();
    }
}
